using System.Xml.Linq;
using X = Adjunct.Framework.Nuspec.XNames.Package.Metadata.References.Reference;

namespace Adjunct.Framework.Nuspec
{
	/// <summary>
	/// Represents a packaged assembly in the 'lib' folder that should be added as a project reference.
	/// http://docs.nuget.org/docs/creating-packages/creating-and-publishing-a-package#Grouping_Assemblies_by_Framework_Version
	/// </summary>
	/// <remarks></remarks>
	public class NuspecLibReference
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="NuspecLibReference"/> class.
		/// </summary>
		/// <param name="filename">The filename.</param>
		/// <remarks></remarks>
		public NuspecLibReference(string filename = null)
		{
			Filename = filename;
		}

		/// <summary>
		/// Gets or sets the filename of the assembly to reference.
		/// </summary>
		/// <value>The filename.</value>
		/// <remarks></remarks>
		public string Filename { get; set; }

		/// <summary>
		/// Clones this instance.
		/// </summary>
		/// <returns></returns>
		/// <remarks></remarks>
		public NuspecLibReference Clone()
		{
			return new NuspecLibReference(Filename);
		}

		/// <summary>
		/// Performs an explicit conversion from <see cref="Adjunct.Framework.Nuspec.NuspecLibReference"/> to
		/// <see cref="System.Xml.Linq.XElement"/>.
		/// </summary>
		/// <param name="reference">The reference.</param>
		/// <returns>The result of the conversion.</returns>
		/// <remarks></remarks>
		public static explicit operator XElement(NuspecLibReference reference)
		{
			return (reference == null) ? null : new XElement(X.ElementName, reference.Filename.ToXAttribute(X.File));
		}

		/// <summary>
		/// Performs an explicit conversion from <see cref="System.Xml.Linq.XElement"/> to
		/// <see cref="Adjunct.Framework.Nuspec.NuspecLibReference"/>.
		/// </summary>
		/// <param name="element">The element.</param>
		/// <returns>The result of the conversion.</returns>
		/// <remarks></remarks>
		public static explicit operator NuspecLibReference(XElement element)
		{
			if (element == null)
			{
				return null;
			}
			element.EnsureElementNameIs(X.ElementName);
			return new NuspecLibReference(element.AttributeValue(X.File));
		}
	}
}